<?php
//订单管理
namespace App\Http\Controllers\App;

use App\Http\Controllers\App\BaseController as Controller;
use App\Models\Order;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class ManagementOrder extends Controller
{
    protected $thisLoginClerk;
    public function __construct(Request $request)
    {
        $checkThisClerk = $this->getShopUserID($request->token,$request->device_id);
        if(!$checkThisClerk){
            return show(2001,'无效token');
        }else{//判断用户角色
            //权限处理
            switch($checkThisClerk->role){
                case -1:
                case 1:
                case 2:
                case 3:
                    $this->thisLoginClerk = $checkThisClerk;
                    break;
                case 0: // 财务没有权限
                    return show(2002,'没有权限');
                    break;
                default:
                    return show(2002,'没有权限');
                    break;
            }
        }
    }

    // 检查订单状态用于支付监听
    public function checkOneOrders(Request $request){
        if(!$request->id){
            return show('2003','请传入验证参数');
        }else{
            $order = Order::find($request->id);
            if($order){
                switch($order->order_type) {
                    case 0 :
                    case 2 :
                    case 3 :
                        if ($order->order_status == 1) {
                            return show('200', '不跳转', 0);
                        } else {
                            return show('200', '跳转', 1);
                        }
                        break;
                    case 1 :
                        if ($order->order_status == 1 || $order->order_status == 3) {
                            return show('200', '不跳转', 0);
                        } else {
                            return show('200', '跳转', 1);
                        }
                        break;
                }
            }
            return show('202','订单信息有误');
        }
    }
    // 获取某个商家的所有订单
    public function getAllOrders(Request $request) {
        $rules = [
            'shop_id'=>'required|integer|exists:shops,id',
            'type'=>'integer|in:0,1,2,3,4,5',
            'status'=>'',//'integer|in:2,3,4,6'
        ];
        $message = [
            'shop_id.*' => '商家信息有误',
            'type.*'=>'订单类型有误',
            'status.*'=>'订单状态有误'
        ];
        $validator = Validator::make($request->all(), $rules,$message);
        if ($validator->fails()) {
            return show(2003,$validator->errors()->first());
        }
        $shop_id = $request->shop_id;//request('shop_id',);
//        order_type  订单类型(0外卖,1预定 ,2排队 ,3扫码 ,4直接付款 ,5充值)
        $type = $request->type;
        $perpage = 4;
        $status = $request->status;
        if (!isset($type)){
            $type = 0;
        }
        if ($status == '9'){
            $status = ['2','3','4','-1','-2','-3'];
        }
        if ($status == '6'){
            $status = ['-1','-2','-3'];
        }
        if (!is_array($status)){
            if (!in_array($status,['2','3','4','6'])){
                return show(2003,'订单状态有误');
            }
        }
        if ($type=='1'){    // 1 预定
            if (is_array($status)){
                $orders = Order::with(['users'=>function($query){
                    $query->select('id','name');
                },'OrderAddress'=>function($query){
                    $query->select('id','address','mobile','name','address_detail');    //,'address_detail'
                },'rorder','OrderDetail'])->select('id','note','user_id','shop_id','order_number','address_id','order_price','order_amount','order_type','order_status','created_at')
                    ->where('shop_id',$shop_id)
                    ->where('order_type',$type)
                    ->whereIn('order_status',$status)
                    ->orderBy('created_at','desc')
                    ->paginate($perpage);
            }else{
                $orders = Order::with(['users'=>function($query){
                    $query->select('id','name');
                },'OrderAddress'=>function($query){
                    $query->select('id','address','mobile','name','address_detail');    //,'address_detail'
                },'rorder','OrderDetail'])->select('id','note','user_id','shop_id','order_number','address_id','order_price','order_amount','order_type','order_status','created_at')
                    ->where('shop_id',$shop_id)
                    ->where('order_type',$type)
                    ->where('order_status',$status)
                    ->orderBy('created_at','desc')
                    ->paginate($perpage);
            }
        }elseif ($type=='0'){   // 配送
            if (is_array($status)){
                $orders = Order::with(['users'=>function($query){
                    $query->select('id','name');
                },'OrderAddress'=>function($query){
                    $query->select('id','address','mobile','name','address_detail');    //
                },'OrderDetail'])->select('id','note','user_id','shop_id','order_number','address_id','order_price','order_amount','order_status','order_type','created_at')
                    ->where('shop_id',$shop_id)
                    ->where('order_type',$type)
                    ->whereIn('order_status',$status)
                    ->orderBy('created_at','desc')
                    ->paginate($perpage);
            }else{
                $orders = Order::with(['users'=>function($query){
                    $query->select('id','name');
                },'OrderAddress'=>function($query){
                    $query->select('id','address','mobile','name','address_detail');
                },'OrderDetail'])->select('id','note','user_id','shop_id','order_number','address_id','order_price','order_amount','order_status','order_type','created_at')
                    ->where('shop_id',$shop_id)
                    ->where('order_type',$type)
                    ->where('order_status',$status)
                    ->orderBy('created_at','desc')
                    ->paginate($perpage);
            }

//            return $order;
        }elseif ($type=='2'){   //排队
            if (is_array($status)){
                $orders = Order::with(['users'=>function($query){
                    $query->select('id','name');
                },'qorder'=>function($query){
//                $query->select('id','eat_time','order_id','number','created_at');
                }])->select('id','note','user_id','shop_id','order_number','address_id','order_price','order_amount','order_type','order_status','created_at')
                    ->where('shop_id',$shop_id)
                    ->where('order_type',$type)
                    ->whereIn('order_status',$status)
                    ->orderBy('created_at','desc')
                    ->paginate($perpage);
            }else{
                $orders = Order::with(['users'=>function($query){
                    $query->select('id','name');
                },'qorder'=>function($query){
//                $query->select('id','eat_time','order_id','number','created_at');
                }])->select('id','note','user_id','shop_id','order_number','address_id','order_price','order_amount','order_type','order_status','created_at')
                    ->where('shop_id',$shop_id)
                    ->where('order_type',$type)
                    ->where('order_status',$status)
                    ->orderBy('created_at','desc')
                    ->paginate($perpage);
            }
        }elseif ($type=='3'){
            if (is_array($status)){
                $orders = Order::with(['users'=>function($query){
                    $query->select('id','name');
                },'OrderDetail'=>function($query){
                    //$query->select('id','address','mobile','name');    //,'address_detail'
                },'orderscan'])->select('id','note','user_id','shop_id','order_number','address_id','order_price','order_amount','order_status','order_type','created_at')
                    ->where('shop_id',$shop_id)
                    ->where('order_type',$type)
                    ->whereIn('order_status',$status)
                    ->orderBy('created_at','desc')
                    ->paginate($perpage);
            }else{
                $orders = Order::with(['users'=>function($query){
                    $query->select('id','name');
                },'OrderDetail','orderscan'])->select('id','note','user_id','shop_id','order_number','address_id','order_price','order_amount','order_status','order_type','created_at')
                    ->where('shop_id',$shop_id)
                    ->where('order_type',$type)
                    ->where('order_status',$status)
                    ->orderBy('created_at','desc')
                    ->paginate($perpage);
            }
//            return '3--扫码';
        }elseif ($type=='4'){
            return '4--直接付款';
        }
//        dd($orders->isEmpty());
        if ($orders->isEmpty()){
            return show(201,'暂无订单信息');
        }
        return show(200,'获取成功',$orders);
    }

    public function getOneOrder(Request $request) {
        $rules = [
            'order_id'=>'required|integer|exists:orders,id',
//            'type'=>'required|integer|in:0,1,2,3,4,5',
//            'shop_id'=>'required|integer|exists:shops,id',
        ];
        $message = [
            'order_id.*' => '订单信息有误',
//            'type.*'=>'订单类型有误',
//            'shop_id.*' => '商家信息有误',
        ];
        $validator = Validator::make($request->all(), $rules,$message);
        if ($validator->fails()) {
            return show(2003,$validator->errors()->first());
        }
//        $shop_id = $request->shop_id;//request('shop_id',);
        try{
            $order_id = $request->order_id;

            $morder = Order::find($order_id);
            if (!$morder){
                return show(201,'暂无该订单信息');
            }
            $type = $morder->order_type;
            $order = [];
            //        order_type  订单类型(0外卖,1预定 ,2排队 ,3扫码 ,4直接付款 ,5充值)
            switch ($type){
                case '0':   // 外卖
                    $order = Order::with(['users'=>function($query){
                        $query->select('id','name');
                    },'OrderAddress'=>function($query){
                        $query->select('id','address','mobile','name','address_detail');    //,
                    },'OrderDetail'=>function($query){
                    }])->find($order_id);
                    break;
                case '1':   // 1预定
                    $order = Order::with(['users'=>function($query){
                        $query->select('id','name');
                    },'orderReserve'=>function($query){
                        // 多规格处理
                    },'OrderDetail'=>function($query){
                    }])->find($order_id);
                    break;
                case '2':       //2排队
                    $order = Order::with(['users'=>function($query){
                        $query->select('id','name');
                    },'OrderAddress'=>function($query){
                        $query->select('id','address','mobile','name','address_detail');    // ,'address_detail'
                    },'qorder'=>function($query){
                        // 多规格处理
                        $query->select('id','order_id','people','eat_time','number','status','created_at');
                    }])->find($order_id);
                    break;
                case '3':
                    $order = Order::with(['users'=>function($query){
                        $query->select('id','name');
                    },'OrderDetail'=>function($query){
                    },'orderscan'])->find($order_id);
                    break;
                case '4':
                    break;
                case '5':
                    break;
            }
            return show(200,'获取成功',$order);
        }catch (\Exception $e){
//            dd($e->getMessage());
            return show(201,'暂无该订单信息');
        }
    }

}
